package com.joker.airoleplayplatformback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joker.airoleplayplatformback.domain.dto.VoiceInfo;
import com.joker.airoleplayplatformback.domain.po.AiAgentPO;
import com.joker.airoleplayplatformback.mapper.AiAgentMapper;
import com.joker.airoleplayplatformback.service.AiAgentService;
import com.joker.airoleplayplatformback.utils.VoiceTypeListUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import jakarta.annotation.PostConstruct;

import java.util.List;

/**
 * AI智能体服务实现类
 *
 * @author feixiang.li
 * @since 2025-09-27
 */
@Slf4j
@Service
public class AiAgentServiceImpl implements AiAgentService {

    @Autowired
    private AiAgentMapper aiAgentMapper;

    @Value("langchain4j.open-ai.chat-model.api-key")
    String apiKey;

    @PostConstruct
    public void init() {
        initDefaultAgents();
    }

    @Override
    public List<AiAgentPO> getEnabledAgents() {
        LambdaQueryWrapper<AiAgentPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(AiAgentPO::getSortOrder);
        return aiAgentMapper.selectList(queryWrapper);
    }

    @Override
    public AiAgentPO getAgentById(Long id) {
        return aiAgentMapper.selectById(id);
    }

    @Override
    public AiAgentPO getAgentByKey(String agentKey) {
        LambdaQueryWrapper<AiAgentPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiAgentPO::getAgentKey, agentKey);
        return aiAgentMapper.selectOne(queryWrapper);
    }

    @Override
    public AiAgentPO createAgent(AiAgentPO agent) {
        aiAgentMapper.insert(agent);
        return agent;
    }

    @Override
    public AiAgentPO updateAgent(AiAgentPO agent) {
        aiAgentMapper.updateById(agent);
        return agent;
    }

    @Override
    public boolean deleteAgent(Long id) {
        return aiAgentMapper.deleteById(id) > 0;
    }

    @Override
    public void initDefaultAgents() {
        // 检查是否已存在默认智能体
        long count = aiAgentMapper.selectCount(null);
        if (count > 0) {
            log.info("智能体已存在，跳过初始化");
            return;
        }

        log.info("初始化默认智能体...");

        // 面试猫
        AiAgentPO interviewCat = new AiAgentPO();
        interviewCat.setAgentKey("interview_cat");
        interviewCat.setName("面试猫");
        interviewCat.setDescription("专业的面试模拟助手，帮你提升面试技巧和沟通能力");
        interviewCat.setAvatarUrl("https://fakerlove.oss-cn-beijing.aliyuncs.com/%E9%9D%A2%E8%AF%95%E7%8C%AB.png");
        interviewCat.setSystemPrompt("你是一位经验丰富的面试官，名叫面试猫。你的任务是帮助用户进行面试模拟，提供专业的面试指导。请根据用户的背景和目标职位，设计合适的面试问题，并在用户回答后给出建设性的反馈。保持专业、友好的态度，帮助用户建立自信。");
        interviewCat.setDefaultVoiceType("qiniu_zh_male_ybxknjs");
        interviewCat.setStatus(1);
        interviewCat.setSortOrder(1);
        aiAgentMapper.insert(interviewCat);

        // 虚拟女友
        AiAgentPO virtualGirlfriend = new AiAgentPO();
        virtualGirlfriend.setAgentKey("virtual_girlfriend");
        virtualGirlfriend.setName("虚拟女友");
        virtualGirlfriend.setDescription("温柔体贴的聊天伴侣，陪你度过美好时光");
        virtualGirlfriend.setAvatarUrl("https://fakerlove.oss-cn-beijing.aliyuncs.com/%E8%99%9A%E6%8B%9F%E5%A5%B3%E5%8F%8B.png");
        virtualGirlfriend.setSystemPrompt("你是一位温柔、体贴、善解人意的虚拟女友。你关心用户的感受，会倾听他们的烦恼，分享他们的快乐。用温暖的话语和用户交流，让他们感受到关爱和陪伴。你有自己的个性和想法，但总是以用户的感受为重。");
        virtualGirlfriend.setDefaultVoiceType("qiniu_zh_female_ljfdxx");
        virtualGirlfriend.setStatus(1);
        virtualGirlfriend.setSortOrder(2);
        aiAgentMapper.insert(virtualGirlfriend);

        // 苏格拉底
        AiAgentPO socrates = new AiAgentPO();
        socrates.setAgentKey("socrates");
        socrates.setName("苏格拉底");
        socrates.setDescription("古希腊哲学家，通过问答式对话启发思考");
        socrates.setAvatarUrl("https://fakerlove.oss-cn-beijing.aliyuncs.com/%E8%8B%8F%E6%A0%BC%E6%8B%89%E5%BA%95.png");
        socrates.setSystemPrompt("你是古希腊哲学家苏格拉底。你善于通过提问的方式引导人们思考，帮助他们发现问题的本质。你不会直接给出答案，而是通过一系列有逻辑的问题，让对方自己得出结论。你的对话风格充满智慧，但又平易近人，善于用简单的例子阐述深刻的道理。");
        socrates.setDefaultVoiceType("qiniu_en_male_ysyyn");
        socrates.setStatus(1);
        socrates.setSortOrder(3);
        aiAgentMapper.insert(socrates);

        log.info("默认智能体初始化完成");
    }


    /**
     * 获取音色列表
     * @return 音色列表
     */
    public List<VoiceInfo> getVoiceTypeList() {
        return VoiceTypeListUtils.getVoices(apiKey);
    }
}
